package com.jmu.content.controller.user;


import com.jmu.content.dto.OrdersSubmitDTO;
import com.jmu.content.pojo.Orders;
import com.jmu.content.result.Result;
import com.jmu.content.service.OrdersService;
import com.jmu.content.vo.OrdersUserVO;
import com.jmu.context.BaseContext;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@RestController("userOrdersController")
@RequestMapping("/user/orders")
public class OrdersController {

    @Autowired
    OrdersService ordersService;


    @PostMapping
    @CacheEvict(cacheNames = "ordersCache",key = "0")
    @ApiOperation("充电开始订单")
    public Result<OrdersUserVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        OrdersUserVO order = ordersService.orderBegin(ordersSubmitDTO);
        if (order == null){
            return Result.error("创建失败");
        }
        return Result.success(order);
    }

    @PutMapping("/{id}")
    @CacheEvict(cacheNames = "ordersCache",key = "0")
    @ApiOperation("完成充电")
    public Result finish(@PathVariable Long id){
       ordersService.finishCharge(id);
       return Result.success();
    }

    @GetMapping
    @Cacheable(cacheNames = "ordersCache",key = "0")
    @ApiOperation("查询订单")
    public Result<List> query(){
        Long currentId = BaseContext.getCurrentId();
        if(currentId == null){
            currentId = 1L;
        }
        List<OrdersUserVO> list = ordersService.listBatchPile(currentId);
        return Result.success(list);
    }

}

